package com.stellar.circle.domain.sCommentLike.impl;

import cn.hutool.core.util.ObjectUtil;
import com.stellar.auth.constants.ErrorCodeConstants;
import com.stellar.circle.basic.entity.SComment;
import com.stellar.circle.basic.entity.SCommentLike;
import com.stellar.circle.basic.entity.SPost;
import com.stellar.circle.basic.entity.SPostLike;
import com.stellar.circle.basic.service.SCommentLikeService;
import com.stellar.circle.basic.service.SCommentService;
import com.stellar.circle.controller.admin.sCommentLike.vo.SCommentLikeRequestPageVO;
import com.stellar.circle.controller.admin.sCommentLike.vo.SCommentLikeRequestVO;
import com.stellar.circle.controller.admin.sCommentLike.vo.SCommentLikeResponseVO;
import com.stellar.circle.convert.SCommentLikeConvert;
import com.stellar.circle.domain.sCommentLike.SCommentLikeDomainService;
import com.stellar.circle.enums.PostLikeTypeEnums;
import com.stellar.core.context.LoginContextHolder;
import com.stellar.web.core.bean.PageResult;
import com.stellar.web.core.enums.IsDeletedFlagEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

import static com.stellar.core.util.ServiceExceptionUtil.exception;

/**
 * 评论点赞表领域服务实现
 *
 * @author Jqchan
 * @version 1.0
 * @date 2025-05-19 14:53:12
 */
@Service
public class SCommentLikeDomainServiceImpl implements SCommentLikeDomainService {

    @Resource
    private SCommentLikeService sCommentLikeService;

    @Resource
    private SCommentService sCommentService;

    @Override
    public PageResult<SCommentLikeResponseVO> page(SCommentLikeRequestPageVO requestPageVO) {
        PageResult<SCommentLikeResponseVO> pageResult = new PageResult<>();
        SCommentLike queryEntity = SCommentLikeConvert.INSTANCE.convert(requestPageVO);
        queryEntity.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getVal());

        List<SCommentLike> entities = sCommentLikeService.page(queryEntity,
                requestPageVO.getStart(), requestPageVO.getPageSize());

        if (entities.isEmpty()) {
            return pageResult;
        }

        Long count = sCommentLikeService.count(queryEntity);
        List<SCommentLikeResponseVO> vos = SCommentLikeConvert.INSTANCE.convertEntityToRespList(entities);

        pageResult.setResult(vos);
        pageResult.setTotal(count.intValue());
        return pageResult;
    }

    @Override
    public Boolean update(SCommentLikeRequestVO requestVO) {
        SCommentLike entity = SCommentLikeConvert.INSTANCE.convert(requestVO);
        return sCommentLikeService.update(entity) != 0;
    }

    @Override
    public Boolean delete(Long id) {
        SCommentLike entity = new SCommentLike();
        entity.setId(id);
        entity.setIsDeleted(IsDeletedFlagEnum.DELETED.getVal());
        return sCommentLikeService.update(entity) != 0;
    }

    @Override
    public Boolean insert(SCommentLikeRequestVO requestVO) {
        SCommentLike entity = SCommentLikeConvert.INSTANCE.convert(requestVO);
        return sCommentLikeService.insert(entity) != 0;
    }

    @Override
    public SCommentLikeResponseVO get(Long id) {
        SCommentLike entity = sCommentLikeService.queryById(id);
        return SCommentLikeConvert.INSTANCE.convert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean like(Long id) {
        try {
            Long userId = LoginContextHolder.getUserId();
            SCommentLike queryEntity = new SCommentLike();
            queryEntity.setCommentId(id);
            queryEntity.setUserId(userId);
            queryEntity.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getVal());
            SCommentLike sCommentLike = sCommentLikeService.queryByCondition(queryEntity);
            SComment sComment = sCommentService.queryById(id);
            if (ObjectUtil.isNull(sCommentLike)) {
                sCommentLikeService.insert(queryEntity);
                sComment.setLikeCount(sComment.getLikeCount() + 1);
            } else {
                sCommentLike.setIsDeleted(IsDeletedFlagEnum.DELETED.getVal());
                sCommentLikeService.update(sCommentLike);
                sComment.setLikeCount(sComment.getLikeCount() - 1);
            }
            sCommentService.update(sComment);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
